package com.chaodows.commons.dto.page;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "hash")
public class Pagination<T> implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(Pagination.class);

    private static final long serialVersionUID = 5788448411192393614L;

    private int page = 1;

    private int total;

    private int pageSize;

    private int maxPage;

    private List<T> items;

    private List<T> testItems;

    private Object testItems2;

    public Pagination() {
    }

    public Pagination(List<T> content, PaginationCondition condition, int total) {
        this.items = content;
        this.pageSize = condition.getPageSize();
        this.page = condition.getPage();
        this.total = total;
        this.testItems = content;
        this.testItems2 = content;

        logger.info("this.items size=" + (this.items == null ? 0 : this.items.size()) + ", this.pageSize=" + this.pageSize + ",this.page=" + this.page + ",this.total=" + this.total);
        logger.info("Pagination this.items size=" + (this.getItems() == null ? 0 : this.getItems().size()) + ", this.pageSize=" + this.pageSize + ",this.getPageSize()=" + this.getPageSize() + ",this.totalthis.getTotal()=" + this.getTotal());
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getMaxPage() {
        if (pageSize == 0)
            return 1;
        maxPage = (total + pageSize - 1) / pageSize;
        if (maxPage <= 0) {
            maxPage = 1;
        }
        return maxPage;
    }

    public void setMaxPage(int maxPage) {
        this.maxPage = maxPage;
    }

    public List<T> getItems() {
        if (items == null) {
            items = new ArrayList<T>();
        }
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }


    public PageInfo<T> generatePageInfo() {
        logger.info("generatePageInfo this.items size=" + (this.items == null ? 0 : this.items.size()) + ", this.pageSize=" + this.pageSize + ",this.page=" + this.page + ",this.total=" + this.total + ",testItems size=" + (this.getTestItems() == null ? 0 : this.getTestItems().size()));
        logger.info("generatePageInfo this.items size=" + (this.getItems() == null ? 0 : this.getItems().size()) + ", this.pageSize=" + this.pageSize + ",this.getPageSize()=" + this.getPageSize() + ",this.totalthis.getTotal()=" + this.getTotal());
        PageInfo<T> info = new PageInfo<T>();
        info.setMaxPage(this.getMaxPage());
        info.setPage(this.getPage());
        info.setPageSize(this.getPageSize());
        info.setTotal(this.getTotal());
        info.setItems(this.getItems());
        logger.info("generatePageInfo2 this.items size=" + (this.items == null ? 0 : this.items.size()) + ", this.pageSize=" + this.pageSize + ",this.page=" + this.page + ",this.total=" + this.total + ",testItems size=" + (this.getTestItems() == null ? 0 : this.getTestItems().size()));
        logger.info("generatePageInfo2 this.items size=" + (this.getItems() == null ? 0 : this.getItems().size()) + ", this.pageSize=" + this.pageSize + ",this.getPageSize()=" + this.getPageSize() + ",this.totalthis.getTotal()=" + this.getTotal());
        logger.info("generatePageInfo2 info.items size=" + (info.getItems() == null ? 0 : info.getItems().size()) + ", this.pageSize=" + this.pageSize + ",this.getPageSize()=" + this.getPageSize() + ",this.totalthis.getTotal()=" + this.getTotal());
        return info;
    }

    public List<T> generateItem() {
        return this.items;
    }

    /**
     * @return
     * @author zhangbingyue
     * @version 创建时间 2013-1-29 下午4:47:17
     */
    public boolean isEmpty() {
        return false;
    }

    @Override
    public String toString() {
        return "Pagination [page=" + page + ", total=" + total + ", pageSize="
                + pageSize + ", maxPage=" + maxPage + ", items=" + items + "]";
    }

    public List<T> getTestItems() {
        return testItems;
    }

    public void setTestItems(List<T> testItems) {
        this.testItems = testItems;
    }

    public Object getTestItems2() {
        return testItems2;
    }

    public void setTestItems2(Object testItems2) {
        this.testItems2 = testItems2;
    }

}
